<template>
	<view class="login">
		<view class="button-group">
			<view>
				<!-- #ifdef MP-WEIXIN -->
				<button @getphonenumber="getPhoneNumber" open-type="getPhoneNumber">一键快速登录</button>
				<!-- #endif -->
				<button @click="goSendCode">手机号登录</button>
			</view>
			<view class="back-home" v-if="review || appid==='wx9528157f77bff5d2'" @click="goHome">取消登录</view>
		</view>
		<uni-popup type="center" ref="confirm" :maskClick="false">
			<view class="confirm-auth">
				<view class="confirm-auth-title">请同意浏览总部商品</view>
				<button open-type="getUserInfo" @getuserinfo="getuserinfo">确定</button>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				review: false,
				code: '',
				phone: '',
				token: '',
				appid: ''
			};
		},
		onLoad() {
			this.review = uni.getStorageSync('review')
			// #ifdef MP-WEIXIN
			this.appid = uni.getExtConfigSync().appid
			uni.login({
				provider: 'weixin',
				success: (loginRes) => {
					this.code = loginRes.code
				}
			})
			// #endif
		},
		onShow() {
			// this.addFoot('登陆页')
		},
		// uni.getUserProfile({
		// 	desc: '用户完善会员资料',
		// 	success: (userInfo) => {
		// 		uni.login({
		// 			success: (loginRes) => {
		// 				let data = {
		// 					code: loginRes.code,
		// 					parent_id: uni.getStorageSync('parent_id')?uni.getStorageSync('parent_id'):'',
		// 					encryptedData: userInfo.encryptedData,
		// 					iv: userInfo.iv,
		// 					phone: this.phone,
		// 					key: this.token
		// 				}
		// 				this.$u.api.WeChatWxLogin(data).then(res => {
		// 					uni.setStorageSync('userInfo',JSON.stringify(res.datas))
		// 					this.loginSuccess(res)
		// 					this.addFoot('登陆页-一键快速登陆','#客户登录了，请注意查看客户详情')
		// 				})
		// 			}
		// 		})
		// 	},
		// 	fail: (fail) => {
		// 		uni.showToast({
		// 			icon: 'none',
		// 			title: '您已拒绝，无法进入总部浏览商品，请重新点击确定授权',
		// 			duration: 3000
		// 		})
		// 	}
		// })
		methods: {
			// 跳转手动输入手机号码登录
			goSendCode() {
				this.addFoot('登陆页-手机号码登陆', '#客户登录了，请注意查看客户详情')
				uni.navigateTo({
					animationDuration: 500,
					url: '/pages/login/sendCode/sendCode'
				})
			},
			// 登录之后获取客服二维码与电话并储存
			getShare() {
				let data = {
					paramter: 1,
					store_id: uni.getStorageSync('userInfo') ? JSON.parse(uni.getStorageSync('userInfo')).store_id : 1
				}
				this.$u.api.StoreInfo(data).then(res => {
					let obj = {
						service_code: res.datas[0].store_service_code || res.datas[0].service_code,
						service_phone: res.datas[0].store_service_phone
					}
					uni.setStorageSync('serviceInfo', obj)
				})
			},
			// 手机号授权快捷登录
			getPhoneNumber(e) {
				if (e.detail.errMsg === 'getPhoneNumber:fail user deny') {
					uni.showToast({
						icon: 'none',
						title: '您已取消授权，无法登录总部，请重新授权'
					})
					return
				}
				uni.checkSession({
					success: () => {
						uni.login({
							provider: 'weixin',
							success: (loginRes) => {
								this.code = loginRes.code
								let data = {
									type: 1,
									code: this.code,
									encryptedData: e.detail.encryptedData,
									iv: e.detail.iv,
									parent_id: uni.getStorageSync('parent_id') ? uni.getStorageSync('parent_id') : '',
								}
								this.$u.api.WeChatBinding(data).then(res => {
									// if ((!res.datas.heard_img && !res.datas.nickname) || !res.datas.form_id) {
									// 	this.phone = res.datas.phone
									// 	this.token = res.datas.token
									// 	this.$refs.confirm.open()
									// 	return
									// } else if ((res.datas.heard_img == 0 && res.datas.nickname == 0) || !res.datas
									// 	.form_id) {
									// 	this.phone = res.datas.phone
									// 	this.token = res.datas.token
									// 	this.$refs.confirm.open()
									// 	return
									// }
									this.loginSuccess(res)
									uni.setStorageSync('userInfo', JSON.stringify(res.datas))
									this.addFoot('登陆页-一键快速登陆', '#客户登录了，请注意查看客户详情')
								})
							},
							fail: (fail) => {
								console.log(fail)
							}
						})
					},
					fail: (fail) => {
						uni.login({
							provider: 'weixin',
							success: (loginRes) => {
								this.code = loginRes.code
								let data = {
									type: 1,
									code: this.code,
									encryptedData: e.detail.encryptedData,
									iv: e.detail.iv,
									parent_id: uni.getStorageSync('parent_id') ? uni.getStorageSync('parent_id') : '',
								}
								this.$u.api.WeChatBinding(data).then(res => {
									if (!res.datas.heard_img && !res.datas.nickname) {
										this.phone = res.datas.phone
										this.token = res.datas.token
										this.$refs.confirm.open()
										return
									} else if (res.datas.heard_img == 0 && res.datas.nickname == 0) {
										this.phone = res.datas.phone
										this.token = res.datas.token
										this.$refs.confirm.open()
										return
									}
									this.loginSuccess(res)
									uni.setStorageSync('userInfo', JSON.stringify(res.datas))
									this.addFoot('登陆页-一键快速登陆', '#客户登录了，请注意查看客户详情')
								})
							}
						})
					}
				})
			},
			// 绑定微信信息
			getuserinfo(e) {
				uni.showLoading({
					title: '请稍后···'
				})
				uni.login({
					provider: 'weixin',
					success: (loginRes) => {
						uni.getUserInfo({
							provider: 'weixin',
							success: (userInfo) => {
								uni.hideLoading()
								let data = {
									code: loginRes.code,
									parent_id: uni.getStorageSync('parent_id') ? uni.getStorageSync('parent_id') : '',
									encryptedData: userInfo.encryptedData,
									iv: userInfo.iv,
									phone: this.phone,
									key: this.token
								}
								this.$u.api.WeChatWxLogin(data).then(res => {
									uni.setStorageSync('userInfo', JSON.stringify(res.datas))
									this.loginSuccess(res)
								})
							},
							fail: (fail) => {
								uni.showToast({
									icon: 'none',
									title: '您已拒绝，无法进入总部浏览商品，请重新点击确定授权',
									duration: 3000
								})
							}
						})
					}
				})
			},
			// 登录成功Function
			loginSuccess(res) {
				uni.showToast({
					icon: 'none',
					title: '登录成功',
					duration: 1500
				})
				setTimeout(() => {
					if (uni.getStorageSync('newurl')) {
						if (uni.getStorageSync('newurl').indexOf('index/index') != -1 || uni.getStorageSync('newurl').indexOf(
								'merchant/merchant') != -1 || uni.getStorageSync('newurl').indexOf('case/case') != -1 || uni
							.getStorageSync('newurl').indexOf('classify/classify') != -1) {
							uni.switchTab({
								url: uni.getStorageSync('newurl'),
								success: () => {
									uni.removeStorageSync('newurl')
								}
							})
						} else {
							uni.redirectTo({
								url: uni.getStorageSync('newurl'),
								success: () => {
									uni.removeStorageSync('newurl')
								}
							})
						}
					} else {
						// #ifdef MP-WEIXIN
						if (uni.getExtConfigSync().appid === 'wx9f2da8a90bb750f7') {
							uni.switchTab({
								url: '/pages/merchant/merchant'
							})
						} else {
							if (res.datas.is_show == 2) {
								uni.switchTab({
									url: '/pages/merchant/merchant'
								})
							} else {
								const pages = getCurrentPages()
								if (pages.length > 1 && pages[pages.length - 2].$vm.$mp.page.route === 'my/my-order/my-order') {
									pages[pages.length - 2].$vm.getData()
								}
								uni.navigateBack()
							}
							if (res.datas.is_open == 1) {
								uni.setStorageSync('is_open', true)
							} else {
								uni.setStorageSync('is_open', false)
							}
						}
						// #endif
						// #ifdef APP-PLUS || H5
						if (res.datas.is_show == 2) {
							uni.switchTab({
								url: '/pages/merchant/merchant'
							})
						} else {
							uni.switchTab({
								url: '/pages/index/index'
							})
						}
						if (res.datas.is_open == 1) {
							uni.setStorageSync('is_open', true)
						} else {
							uni.setStorageSync('is_open', false)
						}
						// #endif
					}
				}, 1500)
				this.$nextTick(() => {
					this.getShare()
					this.$u.api.UserNoticeCount().then(res => {
						if (res.datas) {
							if (res.datas > 99) {
								this.$set(getApp().globalData.defaultMenu[getApp().globalData.defaultMenu.length - 1], 'count',
									'99+')
								this.$set(getApp().globalData.merchantMenu[getApp().globalData.merchantMenu.length - 1], 'count',
									'99+')
							} else {
								this.$set(getApp().globalData.defaultMenu[getApp().globalData.defaultMenu.length - 1], 'count',
									res.datas)
								this.$set(getApp().globalData.merchantMenu[getApp().globalData.merchantMenu.length - 1], 'count',
									res.datas)
							}
						} else {
							this.$set(getApp().globalData.defaultMenu[getApp().globalData.defaultMenu.length - 1], 'count', 0)
							this.$set(getApp().globalData.merchantMenu[getApp().globalData.merchantMenu.length - 1], 'count',
								0)
						}
					})
				}, 200)
			},
			// 返回首页
			goHome() {
				// #ifdef MP-WEIXIN
				if (uni.getExtConfigSync().appid === 'wx9f2da8a90bb750f7') {
					if (this.review) {
						uni.switchTab({
							url: '/pages/classify/classify'
						})
					} else {
						uni.switchTab({
							url: '/pages/merchant/merchant'
						})
					}
				} else {
					uni.navigateBack()
				}
				// #endif
				// #ifdef APP-PLUS || H5
				uni.switchTab({
					url: '/pages/index/index'
				})
				// #endif
			},
			addFoot(where, detail) {
				this.$u.api.AddFoot({
					// #ifdef MP-WEIXIN
					type: 1,
					// #endif
					// #ifdef APP-PLUS
					type: 2,
					// #endif
					// #ifdef H5
					type: 3,
					// #endif
					store_id: 1,
					client: 1,
					where: where,
					stay_time: 0,
					goods_id: 0,
					store_goods_id: 0,
					is_new: 1,
					detail: detail ? detail : ''
				})
			}
		},
	}
</script>

<style lang="scss">
	.login {
		width: 100%;
		width: 100%;
		height: 100vh;
		box-sizing: border-box;
		overflow: hidden;
		background-image: url('https://jiajudashi.oss-cn-shenzhen.aliyuncs.com/static/images/info_img/2020-07-07/20200707/874d33238e9d3caebfc4446d9188af26.jpg?v=1');
		background-size: cover;

		.button-group {
			padding-top: 70vh;
		}

		.button-group button {
			margin: 0 auto;
			width: 500rpx;
			height: 80rpx;
			line-height: 80rpx;
			border-radius: 8rpx;
			font-size: 30rpx;
		}

		.button-group button:first-child {
			background-color: #f59359;
			color: #fff;
		}

		.button-group button:first-child:active {
			opacity: 0.5;
		}

		.button-group button:last-child {
			margin-top: 30rpx;
			background-color: rgba(255, 255, 255, 0.7);
			color: #333;
		}

		.back-home {
			text-align: center;
			color: #FFF;
			margin-top: 30rpx;
		}

	}

	.confirm-auth {
		width: 400rpx;
		background-color: #FFF;
		border-radius: 10rpx;
		padding: 30rpx;
		box-sizing: border-box;

		.confirm-auth-title {
			text-align: center;
			font-size: 32rpx;
			margin-bottom: 30rpx;
		}

		button {
			height: 90rpx;
			line-height: 90rpx;
			background-color: #141414;
			color: #FFF;
			font-size: 30rpx;
		}

		button::after {
			display: none;
		}
	}
</style>